package com.gitee.chessboard.db.impl;

import com.gitee.chessboard.db.MybatisService;
import com.gitee.chessboard.menu.DictionaryManagerMenu;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.project.Project;
import org.apache.ibatis.datasource.DataSourceFactory;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.type.TypeAliasRegistry;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Properties;

/**
 * @author 敲代码的旺财
 * @date 2021/12/22 16:01
 */
public class MybatisServiceImpl implements MybatisService {
    public MybatisServiceImpl(Project project) {
        PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project);
        this.driver = "com.mysql.jdbc.Driver";
        String url = propertiesComponent.getValue(DictionaryManagerMenu.DB_CODE);
        String username = propertiesComponent.getValue(DictionaryManagerMenu.USERNAME_CODE);
        String password = propertiesComponent.getValue(DictionaryManagerMenu.PASSWORD_CODE);
        if (url != null && url.length() > 0
                && username != null && username.length() > 0
                && password != null && password.length() > 0  ){
            this.url = url;
            this.username = username;
            this.password = password;
            init();
        }
    }


    private String driver;
    private String url;
    private String username;
    private String password;

    private SqlSessionFactory sqlSessionFactory;

    public void init() {
        try {
            Configuration configuration = new Configuration();
            TypeAliasRegistry typeAliasRegistry = configuration.getTypeAliasRegistry();


            TransactionFactory transactionFactory = (TransactionFactory) typeAliasRegistry.resolveAlias("JDBC").newInstance();

            Properties properties = new Properties();
            properties.setProperty("driver", driver);
            properties.setProperty("url", url);
            properties.setProperty("username", username);
            properties.setProperty("password", password);
            DataSourceFactory dataSourceFactory = (DataSourceFactory) typeAliasRegistry.resolveAlias("POOLED").newInstance();
            dataSourceFactory.setProperties(properties);
            DataSource dataSource = dataSourceFactory.getDataSource();


            Environment environment = new Environment("development", transactionFactory, dataSource);

            configuration.setEnvironment(environment);

            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            this.sqlSessionFactory = builder.build(configuration);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public String connectTest(){
        try {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            Connection connection = sqlSession.getConnection();
            connection.createStatement().execute("select 1");
            return null;
        }catch (Exception e){
            return "连接失败，"+e.getMessage();
        }
    }
}
